<?php
/**
 *	Akismet spam check for Salesforce.com Web to Lead
 *
 *	@name		Akismet Spam check for Salesforce.com Web to Lead
 *	@version	0.1
 *  @author		Scott Hemmeter
 *  @link		http://www.arrowpointe.com/
 *  @abstract 	This script is intended to capture your web to lead form data and use the Akismet service to 
 * 				determine whether or not it's spam.   It leverages the Akismet PHP5 Class by Alex Potsides
 * 				at http://www.achingbrain.net/.
 *  
 * 				Please note that in order to use this class, you must have a valid {@link http://wordpress.com/api-keys/ WordPress API key}
 * 				They are free for non/small-profit types and getting one will only take a couple of minutes.
 *				For commercial use, please {@link http://akismet.com/commercial/ visit the Akismet commercial 
 * 				licensing page.
 *
 *				Please be aware that this class is PHP5 only.  
 * 				Attempts to run it under PHP4 will most likely fail.
 * 
 *  TODO:  		You need to edit the constants.php file with information specific to your organization
 */

/***  FOR MOST ORGANIZATIONS, THERE SHOULD BE NO NEED TO TOUCH THE CODE BELOW HERE ***/

/********************************************
 * Get Setup 
 ********************************************/
// Make sure cURL is enabled
if (!function_exists('curl_init')) {
	exit("Curl is not setup on this PHP server and is required for this script");
}

// Include important files
include('Akismet.class.php'); // PHP5 class for Akismet.  Found at http://akismet.com/development/. 
include('constants.php'); // File where you put your org specific information

/********************************************
 * Process the data that came in from the W2L form
 ********************************************/
if (isset($_POST)) {
	
	if (count($_POST) == 0) exit("Error.  No data was passed to this script.");
	
	$cleanPOST = array(); // new variable that will hold cleaned up a version of $_POST data
	$content = ""; // the content to send to Akismet for spam checking

	// Loop through the $_POST data and process it different whether get_magic_quotes is enabled or not
	foreach ($_POST as $key=>$value){
		if (get_magic_quotes_gpc() == 0) {
			$cleanPOST[stripslashes($key)] = $value;
			if (!in_array($key, $Akismet_noPass)) $content .= " " . $value;
		} else {
			$cleanPOST[stripslashes($key)] = stripslashes($value);
			if (!in_array($key, $Akismet_noPass)) $content .= " " . stripslashes($value);
		}
	}

	// Add the Org ID if it's not passed into the script from the W2L form
	if (!isset($_POST["oid"])) $cleanPOST["oid"] = $oid;
	
} else {
	exit("Error.  No data was passed to this script.");
}

/********************************************
 * Determine whether it's spam or not.
 ********************************************/
// Setup Author Name field to pass to Akismet
$LeadName = "";
	if (isset($_POST["first_name"])) $LeadName .= $_POST["first_name"];
	if (isset($_POST["last_name"])) $LeadName .= " " . $_POST["last_name"];

// Setup the Author Email field to pass to Akismet
$LeadEmail = "";
	if (isset($_POST["email"])) $LeadEmail .= $_POST["email"];

// Setup the Author URL field to pass to Akismet
$LeadURL = "";
	if (isset($_POST["URL"])) $LeadURL .= $_POST["URL"];

$akismet = new Akismet($MyURL ,$WordPressAPIKey); // initiate a session with Akismet
$akismet->setCommentAuthor($LeadName);
$akismet->setCommentAuthorEmail($LeadEmail);
$akismet->setCommentAuthorURL($LeadURL);
$akismet->setCommentContent($content);

// This is supposed to be the location of your web form.  It's optional.
//$akismet->setPermalink('http://www.example.com/blog/alex/someurl/');

if($akismet->isCommentSpam()) {
    // This is potential spam
	$cleanPOST[$SFDC_Akismet_Field] = "1";
} else {
    // This is not likely spam
	$cleanPOST[$SFDC_Akismet_Field] = "0";
}

/********************************************
 * Use cURL to post to Salesforce web to lead 
 ********************************************/
	// Create a new cURL resource
	$ch = curl_init();	

	// Set Options
		
		// Point to the Salesforce Web to Lead page
		curl_setopt($ch, CURLOPT_URL, "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8");
		
		// Set the method to POST
		curl_setopt($ch, CURLOPT_POST, 1);
		
		// Pass POST data
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
		
	
	curl_exec($ch); // Post to Salesforce
	
	curl_close($ch); // close cURL resource
	
?>